package com.wh.service.instorehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.api.Api;
import com.wh.mapper.instorehouse.PurchaseMapper;
import com.wh.mapper.instorehouse.ReceivingMapper;
import com.wh.mapper.storehousemanage.RemoveMapper;
import com.wh.pojo.Purchase;
import com.wh.pojo.Receiving;
import com.wh.pojo.Remove;
import com.wh.vo.Options;
import com.wh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReceivingServiceImpl implements ReceivingService{

    @Autowired
    private ReceivingMapper receivingMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private RemoveMapper removeMapper;

    @Override
    public List<Options> getOptions() {
        List<Options> optionsList = new ArrayList<>();
        List<Options> optionsList2 = new ArrayList<>();
        List<Options> optionsList3 = new ArrayList<>();
        Options optionsPurchase = new Options();
        Options optionsRemove = new Options();
        QueryWrapper<Purchase> purchaseQueryWrapper = new QueryWrapper<>();
        purchaseQueryWrapper.eq("status","已审核");


        List<Purchase> purchaseList = purchaseMapper.selectList(purchaseQueryWrapper);
//        System.out.println(purchaseList);
//        System.out.println(purchaseList.get(0).getPurchaseId());

        for(Purchase purchase : purchaseList){
            optionsList2.add(new Options().setLabel(purchase.getPurchaseId()).setValue(purchase.getPurchaseId()));
        }
        optionsPurchase.setValue("purchase").setLabel("采购订单编号").setChildren(optionsList2);
//        System.out.println(optionsList2);
        optionsList.add(optionsPurchase);

        QueryWrapper<Remove> removeQueryWrapper = new QueryWrapper<>();
        removeQueryWrapper.eq("status","已审核");
        List<Remove> removeList = removeMapper.selectList(removeQueryWrapper);
        for(Remove remove : removeList){
            optionsList3.add(new Options().setLabel(remove.getRemoveId()).setValue(remove.getRemoveId()));
        }
        optionsRemove.setValue("remove").setLabel("移库编号").setChildren(optionsList3);



        optionsList.add(optionsRemove);

        System.out.println(optionsList);
        return optionsList;
    }

    @Override
    public void addReceiving(String id,String user) {


//        System.out.println(purchase);

        Receiving receiving = new Receiving();
        receiving.setReceivingId("IR" + Long.toString(System.currentTimeMillis()))
                .setReceivingPerson(user)
                .setReceivingTime(new Api().getTime())
                .setStatus("待审核");


        if (id.charAt(0) == 'I'){
            Purchase purchase = purchaseMapper.selectById(id);
            receiving.setPurchaseId(purchase.getPurchaseId());
            purchase.setStatus("进行中");
            purchaseMapper.updateById(purchase);
        }else {
            Remove remove = removeMapper.selectById(id);
            receiving.setRemoveId(remove.getRemoveId());
            remove.setStatus("进行中");
            removeMapper.updateById(remove);
        }
        receivingMapper.insert(receiving);

    }

    @Override
    public PageResult getReceiving(PageResult pageResult) {
        Page<Receiving> receivingPage = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Receiving> queryWrapper = new QueryWrapper<>();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"receiving_id",pageResult.getQuery());

        receivingPage = receivingMapper.selectPage(receivingPage,queryWrapper);
        pageResult.setTotal((long) receivingPage.getRecords().size()).setRows(receivingPage.getRecords());
//        System.out.println(receivingPage.getRecords().size());
        return pageResult;
    }

    @Override
    public void updataReceiving(Receiving receiving) {
        if (StringUtils.hasLength(receiving.getCheckPerson())){
            receiving.setCheckTime(new Api().getTime());
        }
        receivingMapper.updateById(receiving);
    }

    @Override
    public void deleteReceiving(String receivingId,String purchaseId) {
        receivingMapper.deleteById(receivingId);
        Purchase purchase = purchaseMapper.selectById(purchaseId);
        purchase.setStatus("已审核");
        purchaseMapper.updateById(purchase);

    }
    

}
